package com.hikvision.open.app.ui.broadcast

import android.Manifest
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.Observer
import com.hikvision.hatomplayer.PlayCallback
import com.hikvision.open.app.utils.PermissionHelper
import com.hikvision.open.app.utils.inflate
import com.hikvision.open.app.widget.PermissionDialog
import hik.sdk.demo.R
import hik.sdk.demo.databinding.ActivityBroadcastBinding

/**
 * <p> 广播界面 </p>
 * @author 段自航 2021/7/14 10:20
 * @version V1.0
 */
class BroadcastActivity : AppCompatActivity(), View.OnClickListener {
    private val viewModel by viewModels<BroadcastViewModel>()
    private val viewBinding: ActivityBroadcastBinding by inflate()
    private var broadcastUrl =
        "rtsp://10.19.189.162:655/dac/broadcast/TCP?priority=10&encode=MP3&sample=3"
    private var broadcastIndexCodes = "3d69e31b9d094dce8617b2c3768589a2"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(viewBinding.root)
        viewBinding.backImage.setOnClickListener {
            finish()
        }
        viewBinding.broadcastUrlEdit.setText(broadcastUrl)
        viewBinding.indexCodesEdit.setText(broadcastIndexCodes)
        viewBinding.startBroadcastButton.setOnClickListener(this)
        viewBinding.stopBroadcastButton.setOnClickListener(this)
        viewModel.broadcastResult.observe(this, Observer {
            when (it.status) {
                PlayCallback.Status.SUCCESS -> {
                    Toast.makeText(this, "广播开启成功", Toast.LENGTH_SHORT).show()
                }
                PlayCallback.Status.FAILED -> {
                    Toast.makeText(this, "广播开启失败", Toast.LENGTH_SHORT).show()
                }
                PlayCallback.Status.EXCEPTION -> {
                    Toast.makeText(this, "广播异常", Toast.LENGTH_SHORT).show()
                }
                else -> {

                }
            }
        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.startBroadcastButton -> {
                startBroadcast()
            }
            R.id.stopBroadcastButton -> {
                stopBroadcast()
            }
        }
    }

    /**
     * 开启广播
     */
    private fun startBroadcast() {
        val broadcastUrl = viewBinding.broadcastUrlEdit.text.toString()
        if (broadcastUrl.isEmpty()) {
            Toast.makeText(this, "请输入广播Url", Toast.LENGTH_SHORT).show()
            return
        }
        val broadcastIndexCodes = viewBinding.indexCodesEdit.text.toString()
        if (broadcastIndexCodes.isEmpty()) {
            Toast.makeText(this, "请输入广播监控点数据", Toast.LENGTH_SHORT).show()
            return
        }
        //进行权限判断
        if (PermissionHelper.isGranted(this, Manifest.permission.RECORD_AUDIO)) {
            doOpenBroadcast(broadcastUrl, broadcastIndexCodes)
        } else {
            //请求权限
            PermissionHelper.requestRecordAudioPermission(this) { allGranted ->
                if (allGranted) {
                    doOpenBroadcast(broadcastUrl, broadcastIndexCodes)
                }
            }
        }

    }

    private fun doOpenBroadcast(
        broadcastUrl: String,
        broadcastIndexCodes: String
    ) {
        val split = broadcastIndexCodes.split(",")
        if (split.isEmpty()) {
            return
        }
        val indexCodes = mutableListOf<String>()
        split.forEach {
            indexCodes.add(it)
        }
        if (indexCodes.isEmpty()) {
            return
        }
        viewModel.startBroadcast(broadcastUrl, indexCodes)
    }

    /**
     * 停止广播
     */
    private fun stopBroadcast() {
        viewModel.stopBroadcast()
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        PermissionHelper.handlePermissionsResult(requestCode, permissions, grantResults)
    }
}